package com.dashu.device.tools;


import com.dashu.base.bean.device.DeviceBase;
import com.dashu.base.bean.device.DeviceDownBase;
import com.dashu.base.bean.device.req.*;
import com.dashu.base.enums.device.DeviceType;
import com.dashu.base.enums.device.DownType;
import com.dashu.base.utils.RegularUtils;
import org.apache.http.util.TextUtils;

import static com.dashu.base.api.device.DeviceStatus.*;

/**
 * 校验参数
 */
public class CheckParams {

    /**
     * 添加图片的参数校验
     *
     * @param req
     * @return
     */
    public static DeviceDownBase addPic(DevicePicAddReq req) {
        DeviceDownBase downBase = new DeviceDownBase();
        //默认成功
        downBase.setMsg(SUCCESS);
        downBase.setMsg_en(SUCCESS_EN);
        downBase.setStatus(SUCCESS_STATUS);

        //判断设备数据是否为空
        if (req == null || checkObject(downBase, req.getBeans())) return downBase;


        //检查下发类型是否合法
        downBase = checkDownBase(downBase , req);
        if (downBase.getStatus() != SUCCESS_STATUS)
            return downBase;

        if (TextUtils.isEmpty(req.getPicContent())){
            downBase.setMsg(PARAMS_NULL + "(图片内容为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "pic content is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getPicType() == null){
            downBase.setMsg(PARAMS_NULL + "(图片类型为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "pic type is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getIden() == null){
            downBase.setMsg(PARAMS_NULL + "(身份类型为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "iden type is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getPersonId() == null){
            downBase.setMsg(PARAMS_NULL + "(用户唯一标识为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "personId is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }

        if (req.getDownType() != DownType.ADDPIC){
            downBase.setMsg(DOWNTYPE_FUNCTION_FAIL);
            downBase.setMsg_en(DOWNTYPE_FUNCTION_FAIL_EN);
            downBase.setStatus(DOWNTYPE_FUNCTION_FAIL_STATUS);
            return downBase;
        }
//        if (req.getPicType() == PicType.URL)
//            if (!RegularUtils.isUrl(req.getPicContent())){
//                downBase.setMsg(URL_INVALID);
//                downBase.setMsg_en(URL_INVALID_EN);
//                downBase.setStatus(URL_INVALID_STATUS);
//                return downBase;
//            }

        return checkDevice(downBase , req.getBeans(), req.getDeviceType());
    }


    /**
     * 删除图片的参数校验
     *
     * @param req
     * @return
     */
    public static DeviceDownBase delPic(DevicePicDelReq req) {
        DeviceDownBase downBase = new DeviceDownBase();
        //默认成功
        downBase.setMsg(SUCCESS);
        downBase.setMsg_en(SUCCESS_EN);
        downBase.setStatus(SUCCESS_STATUS);

        if (req == null || checkObject(downBase, req.getBeans())) return downBase;

        //检查下发类型是否合法
        downBase = checkDownBase(downBase , req);
        if (downBase.getStatus() != SUCCESS_STATUS)
            return downBase;

        if (TextUtils.isEmpty(req.getPersonId())) {
            downBase.setMsg(PARAMS_NULL + "(用户唯一标识为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(personId is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getIden() == null){
            downBase.setMsg(PARAMS_NULL + "(用户身份为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(iden is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getDownType() != DownType.DELPIC){
            downBase.setMsg(DOWNTYPE_FUNCTION_FAIL);
            downBase.setMsg_en(DOWNTYPE_FUNCTION_FAIL_EN);
            downBase.setStatus(DOWNTYPE_FUNCTION_FAIL_STATUS);
            return downBase;
        }
        return checkDevice(downBase , req.getBeans(), req.getDeviceType());
    }


    /**
     * 修改图片的参数校验
     *
     * @param req
     * @return
     */
    public static DeviceDownBase updPic(DevicePicUpdReq req) {
        DeviceDownBase downBase = new DeviceDownBase();

        //默认成功
        downBase.setMsg(SUCCESS);
        downBase.setMsg_en(SUCCESS_EN);
        downBase.setStatus(SUCCESS_STATUS);

        if (req == null || checkObject(downBase, req.getBeans())) return downBase;

        //检查下发类型是否合法
        downBase = checkDownBase(downBase , req);
        if (downBase.getStatus() != SUCCESS_STATUS)
            return downBase;

        if (TextUtils.isEmpty(req.getPicContent())){
            downBase.setMsg(PARAMS_NULL + "(图片内容为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "pic content is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getPicType() == null){
            downBase.setMsg(PARAMS_NULL + "(图片类型为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "pic type is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getIden() == null){
            downBase.setMsg(PARAMS_NULL + "(身份类型为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "iden type is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (req.getPersonId() == null){
            downBase.setMsg(PARAMS_NULL + "(用户唯一标识为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "personId is null");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }

        if (req.getDownType() != DownType.UDPPIC){
            downBase.setMsg(DOWNTYPE_FUNCTION_FAIL);
            downBase.setMsg_en(DOWNTYPE_FUNCTION_FAIL_EN);
            downBase.setStatus(DOWNTYPE_FUNCTION_FAIL_STATUS);
            return downBase;
        }

//        if (req.getPicType() == PicType.URL)
//            if (!RegularUtils.isUrl(req.getPicContent())){
//                downBase.setMsg(URL_INVALID);
//                downBase.setMsg_en(URL_INVALID_EN);
//                downBase.setStatus(URL_INVALID_STATUS);
//                return downBase;
//            }


        return checkDevice(downBase , req.getBeans(), req.getDeviceType());
    }


    /**
     * 修改图片的参数校验
     *
     * @param req
     * @return
     */
    public static DeviceDownBase getPic(DevicePicListReq req) {
        DeviceDownBase downBase = new DeviceDownBase();
        //默认成功
        downBase.setMsg(SUCCESS);
        downBase.setMsg_en(SUCCESS_EN);
        downBase.setStatus(SUCCESS_STATUS);

        if (req == null || checkObject(downBase, req.getBeans())) return downBase;

        //检查下发类型是否合法
        downBase = checkDownBase(downBase , req);
        if (downBase.getStatus() != SUCCESS_STATUS)
            return downBase;

        if (req.getDownType() != DownType.GLIPIC){
            downBase.setMsg(DOWNTYPE_FUNCTION_FAIL);
            downBase.setMsg_en(DOWNTYPE_FUNCTION_FAIL_EN);
            downBase.setStatus(DOWNTYPE_FUNCTION_FAIL_STATUS);
            return downBase;
        }

        return checkDevice(downBase , req.getBeans(), req.getDeviceType());
    }


    /**
     * 远程开门的参数校验
     *
     * @param req
     * @return
     */
    public static DeviceDownBase openDoor(DeviceOpenDoorReq req) {
        DeviceDownBase downBase = new DeviceDownBase();
        //默认成功
        downBase.setMsg(SUCCESS);
        downBase.setMsg_en(SUCCESS_EN);
        downBase.setStatus(SUCCESS_STATUS);

        if (req == null || checkObject(downBase, req.getBeans())) return downBase;

        //检查下发类型是否合法
        downBase = checkDownBase(downBase , req);
        if (downBase.getStatus() != SUCCESS_STATUS)
            return downBase;

        if (req.getDownType() != DownType.OPEDOR){
            downBase.setMsg(DOWNTYPE_FUNCTION_FAIL);
            downBase.setMsg_en(DOWNTYPE_FUNCTION_FAIL_EN);
            downBase.setStatus(DOWNTYPE_FUNCTION_FAIL_STATUS);
            return downBase;
        }
        return checkDevice(downBase , req.getBeans() , req.getDeviceType());
    }


    /**
     * 判断下发基础类型
     * @param resp
     * @param deviceDownBase
     * @return
     */
    private static DeviceDownBase checkDownBase(DeviceDownBase resp , DeviceDownBase deviceDownBase){
        if (deviceDownBase.getModuleType() == null){
            resp.setMsg(PARAMS_NULL + "(模块类型为空)");
            resp.setMsg_en(PARAMS_NULL_EN + "(module type is null)");
            resp.setStatus(PARAMS_NULL_STATUS);
            return resp;
        }
        if (deviceDownBase.getDownType() == null){
            resp.setMsg(PARAMS_NULL + "(下发类型为空)");
            resp.setMsg_en(PARAMS_NULL_EN + "(down type is null)");
            resp.setStatus(PARAMS_NULL_STATUS);
            return resp;
        }
        return resp;
    }


    /**
     * 检测对象是否为空
     *
     * @param downBase
     * @param beans
     * @return
     */
    private static boolean checkObject(DeviceDownBase downBase, DeviceBase[] beans) {
        if (beans == null || beans.length == 0) {
            downBase.setMsg(PARAMS_NULL + "(设备数组为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(device array is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return true;
        }
        return false;
    }


    /**
     * 检测设备是否合法
     *
     * @param deviceBases
     * @param deviceType
     * @return
     */
    private static DeviceDownBase checkDevice(DeviceDownBase downBase , DeviceBase[] deviceBases, DeviceType deviceType) {
        for (DeviceBase deviceBase : deviceBases) {
            if (deviceBase == null) {
                downBase.setMsg(PARAMS_NULL + "(设备对象为空)");
                downBase.setMsg_en(PARAMS_NULL_EN + "device object is null");
                downBase.setStatus(PARAMS_NULL_STATUS);
                return downBase;
            }
//            if (deviceBase.getDeviceType() == null){
//                downBase.setMsg(PARAMS_NULL + "(设备类型为空)");
//                downBase.setMsg_en(PARAMS_NULL_EN + "(device type is null)");
//                downBase.setStatus(PARAMS_NULL_STATUS);
//                return downBase;
//            }
//            if (deviceBase.getProtocolType() == null){
//                downBase.setMsg(PARAMS_NULL + "(协议类型为空)");
//                downBase.setMsg_en(PARAMS_NULL_EN + "(protocol type is null)");
//                downBase.setStatus(PARAMS_NULL_STATUS);
//                return downBase;
//            }

            //不合法  ip和端口
            if (deviceBase.getPort() <= 0 || deviceBase.getPort() >= 65535 || TextUtils.isEmpty(deviceBase.getIp()) || !RegularUtils.isIpv4(deviceBase.getIp())) {
                downBase.setMsg(PARAMS_NULL + "(端口或者IP不合法)");
                downBase.setMsg_en(PARAMS_NULL_EN + "(port or ip is invalid)");
                downBase.setStatus(PARAMS_NULL_STATUS);
                return downBase;
            }
            if (deviceType == DeviceType.JL_CONTROL)
                return JL(downBase , deviceBase);


            if (deviceType == DeviceType.HQ_CONTROL)
                return HQ(downBase , deviceBase);
        }
        System.out.println("55555555555555555555555555555555555555555555555555555555555555");
        return downBase;
    }

    /**
     * 巨龙设备判断
     * @param deviceBase
     * @return
     */
    private static DeviceDownBase JL(DeviceDownBase downBase , DeviceBase deviceBase) {
        if (TextUtils.isEmpty(deviceBase.getDeviceUUID())) {
            downBase.setMsg(PARAMS_NULL + "(巨龙设备UUID为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(jl device uuid is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        return downBase;
    }

    /**
     * 海清设备判断
     * @param deviceBase
     * @return
     */
    private static DeviceDownBase HQ(DeviceDownBase downBase, DeviceBase deviceBase) {
        if (TextUtils.isEmpty(deviceBase.getDeviceUUID())) {
            downBase.setMsg(PARAMS_NULL + "(海清设备UUID为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(hq device uuid is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        if (TextUtils.isEmpty(deviceBase.getDeviceUser())) {
            downBase.setMsg(PARAMS_NULL + "(海清设备用户为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(hq device user is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }

        if (TextUtils.isEmpty(deviceBase.getDevicePassword())) {
            downBase.setMsg(PARAMS_NULL + "(海清设备密码为空)");
            downBase.setMsg_en(PARAMS_NULL_EN + "(hq device password is null)");
            downBase.setStatus(PARAMS_NULL_STATUS);
            return downBase;
        }
        return downBase;
    }
}
